﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using Settings = HeThongDoDien.Properties.Settings;

namespace HeThongDoDien
{
    public partial class frmConfiguration : Form
    {
        public frmConfiguration()
        {
            InitializeComponent();
        }

        #region Local Fields

        private Int32[] bitRates;
        private string[] portNames;

        #endregion

        #region Private Methods

        private void InitializeField()
        {
            bitRates = new int[11];
            bitRates[0] = 300;
            bitRates[1] = 600;
            bitRates[2] = 1200;
            bitRates[3] = 2400;
            bitRates[4] = 9600;
            bitRates[5] = 14400;
            bitRates[6] = 19200;
            bitRates[7] = 38400;
            bitRates[8] = 57600;
            bitRates[9] = 115200;
            bitRates[10] = 128000;
            // Baud Rate
            this.cboForceBaudRate.DataSource = bitRates;
            this.cboDistanceBaudRate.DataSource = bitRates.Clone();
            this.cboForceBaudRate.SelectedItem = bitRates[4];
            this.cboDistanceBaudRate.SelectedItem = bitRates[4];
            // PortName
            this.portNames = SerialPort.GetPortNames();
            var plen = portNames.Length;
            // Bind DataSource
            this.cboForcePortName.DataSource = portNames;
            this.cboDistancePortName.DataSource = portNames.Clone();
            // Is there at least two COM port?
            if (plen > 1)
            {
                this.lbNoCOMPorts.Visible = false;
                this.btnOK.Enabled = true;
                var forcePortName = string.IsNullOrEmpty(Settings.Default.ForcePortName) ? portNames[0] : Settings.Default.ForcePortName;
                var distancePortName = string.IsNullOrEmpty(Settings.Default.DistancePortName) ? portNames[1] : Settings.Default.DistancePortName;
                var forceBaudRate = Settings.Default.ForceBaudRate;
                var distanceBaudRate = Settings.Default.DistanceBaudRate;
                // Com port
                this.cboForcePortName.SelectedItem = forcePortName;
                this.cboDistancePortName.SelectedItem = distancePortName;
                // Baud Rate
                this.cboForceBaudRate.SelectedItem = forceBaudRate;
                this.cboDistanceBaudRate.SelectedItem = distanceBaudRate;
            }
            else
            {
                this.lbNoCOMPorts.Visible = true;
                this.btnOK.Enabled = false;
            }

        }

        private void InitializeForm()
        {
            this.AcceptButton = this.btnOK;
            this.CancelButton = this.btnCancel;
            this.btnOK.DialogResult = DialogResult.OK;
            this.btnCancel.DialogResult = DialogResult.Cancel;
        }

        private void SavePreferences()
        {
            //if (this.cboForcePortName.SelectedIndex == this.cboDistancePortName.SelectedIndex)
            //{
            //    throw new Exception("Two COM ports must be distinct");
            //}
            if (this.cboForcePortName.SelectedIndex > -1 && this.cboDistancePortName.SelectedIndex > -1)
            {
                Settings.Default.ForcePortName = this.cboForcePortName.SelectedItem as string;
                Settings.Default.DistancePortName = this.cboDistancePortName.SelectedItem as string;
                Settings.Default.ForceBaudRate = (int)this.cboForceBaudRate.SelectedItem;
                Settings.Default.DistanceBaudRate = (int)this.cboDistanceBaudRate.SelectedItem;
                Settings.Default.Save();
            }
        }
        #endregion

        private void frmConfiguration_Load(object sender, EventArgs e)
        {
            // AssignEvent
            this.FormClosing += new FormClosingEventHandler(frmConfiguration_FormClosing);
            // LoadComponent
            this.InitializeField();
            this.InitializeForm();
        }

        private void frmConfiguration_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.OK)
                this.SavePreferences();
        }

        private void cboForcePortName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.portNames.Length > 0 && this.cboForcePortName.SelectedIndex == this.cboDistancePortName.SelectedIndex)
                {
                    var cindex = Array.IndexOf(this.portNames, this.cboDistancePortName.SelectedItem as string);
                    var nindex = (cindex + 1) % this.portNames.Length;
                    this.cboDistancePortName.SelectedItem = this.portNames[nindex];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cboDistancePortName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.portNames.Length > 0 && this.cboForcePortName.SelectedIndex == this.cboDistancePortName.SelectedIndex)
                {
                    var cindex = Array.IndexOf(this.portNames, this.cboForcePortName.SelectedItem as string);
                    var nindex = (cindex + 1) % this.portNames.Length;
                    this.cboForcePortName.SelectedItem = this.portNames[nindex];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
